package com.middlegroup.pay.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.middlegroup.pay.entity.SystemCfg;
import com.middlegroup.pay.mapper.SystemCfgMapper;
import com.middlegroup.pay.service.SystemCfgService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by pc on 2019/1/15.
 */
@Service
public class SystemServiceImpl extends ServiceImpl<SystemCfgMapper, SystemCfg> implements SystemCfgService {

    @Resource
    private SystemCfgMapper systemCfgMapper;

    @Override
    @Cacheable(value = "pay_platform", key = "'system:config'", unless = "#result eq null")
    public Map<String, Map<String, String>> getConfigs() {
        Map<String, Map<String, String>> configs = null;
        //查询所有配置
        List<SystemCfg> configLists = systemCfgMapper.selectList(null);
        if (!CollectionUtils.isEmpty(configLists)) {
            configs = new HashMap<>();
            for (SystemCfg systemCfg : configLists) {
                if (configs.containsKey(systemCfg.getCategory())) {
                    //如果放入了则去除对应category的map
                    Map<String, String> config = configs.get(systemCfg.getCategory());
                    if (config.containsKey(systemCfg.getName())) {
                        //有则跳过
                        continue;
                    } else {
                        //无则put
                        config.put(systemCfg.getName(), systemCfg.getContent());
                    }
                } else {
                    Map<String, String> config = new HashMap<>();
                    config.put(systemCfg.getName(), systemCfg.getContent());
                    configs.put(systemCfg.getCategory(), config);
                }
            }
        }
        return configs;
    }

    @Override
    @CachePut(value = "pay_platform", key = "'system:config'", unless = "#result eq null")
    public Map<String, Map<String, String>> refreshConfigs() {
        Map<String, Map<String, String>> configs = null;
        //查询所有配置
        List<SystemCfg> configLists = systemCfgMapper.selectList(null);
        if (!CollectionUtils.isEmpty(configLists)) {
            configs = new HashMap<>();
            for (SystemCfg systemCfg : configLists) {
                if (configs.containsKey(systemCfg.getCategory())) {
                    //如果放入了则去除对应category的map
                    Map<String, String> config = configs.get(systemCfg.getCategory());
                    if (config.containsKey(systemCfg.getName())) {
                        //有则跳过
                        continue;
                    } else {
                        //无则put
                        config.put(systemCfg.getName(), systemCfg.getContent());
                    }
                } else {
                    Map<String, String> config = new HashMap<>();
                    config.put(systemCfg.getName(), systemCfg.getContent());
                    configs.put(systemCfg.getCategory(), config);
                }
            }
        }
        return configs;
    }

    @Override
    @Cacheable(value = "pay_platform", key = "'system:config:'+#p0.toString()+#p1.toString()", unless = "#result eq null")
    public SystemCfg getConfig(String category, String name) {
        SystemCfg systemCfgFind = new SystemCfg();
        systemCfgFind.setCategory(category);
        systemCfgFind.setName(name);
        SystemCfg systemCfg = systemCfgMapper.selectOne(systemCfgFind);
        return systemCfg;
    }

    @Override
    public String getConfigString(String category, String name) {
       SystemCfg systemCfg = getConfig(category,name);
       if (null != systemCfg){
           return systemCfg.getContent();
       }
       return null;
    }
}
